package com.wing.product.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.product.model.response.ScanDeliveryVo;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductPackLabel;
import com.wing.product.model.response.ProductPackLabelVO;
import com.wing.product.service.ProductPackLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.product.model.request.ProductPackLabelForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-产品包装条码管理", tags = "产品包装条码管理")
@RestController("appProductPackLabelController")
@RequestMapping("product/app/productPackLabel")
public class ProductPackLabelController {

    @Autowired
    private ProductPackLabelService productPackLabelService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询所有产品包装条码", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", example = "desc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "status", value="状态", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductPackLabelVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String status
    ){

        Page<ProductPackLabelVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if(StringUtils.isNotBlank(status)){
            params.put("status",status);
        }
        //搜索
        IPage<ProductPackLabelVO> list = productPackLabelService.pageList4app(pg, params);
        List<ProductPackLabelVO> records = list.getRecords();
        for (ProductPackLabelVO productPackLabelVO: records) {
            productPackLabelVO.setInventoryTypeIdDesc(productPackLabelVO.getInventoryTypeId().getDesc());
        }

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @PostMapping("/add")
    public JsonResult add(@Validated ProductPackLabelForm productPackLabelForm) {

        if (productPackLabelService.add(productPackLabelForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }

        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @PutMapping("/saveSign")
    public JsonResult saveSign(@RequestBody ScanDeliveryVo scanDeliveryVo){

        ProductPackLabel productPackLabel = new ProductPackLabel();
//        productPackLabel.setId(scanDeliveryVo.getId());
        productPackLabel.setSurplusQuantity(scanDeliveryVo.getRealReceive());
        productPackLabel.setQuantity(scanDeliveryVo.getRealReceive());
        productPackLabel.setSkuCode(scanDeliveryVo.getSkuCode());
        productPackLabel.setBarcode(scanDeliveryVo.getPackLabelCode());
        productPackLabel.setCode(scanDeliveryVo.getPackLabelCode());
        productPackLabel.setQuantity(scanDeliveryVo.getRealReceive());
        productPackLabel.setSurplusQuantity(scanDeliveryVo.getRealReceive());
        productPackLabel.setBatchCode(scanDeliveryVo.getBatchCode());
        //查询是否存在
            productPackLabelService.save(productPackLabel);
            return JsonResult.success();
    }

    @GetMapping("/getLabelCode")
    public JsonResult getLabelCode(@RequestParam("packLabelCode")String packLabelCode){

        QueryWrapper<ProductPackLabel>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("barcode",packLabelCode);
        ProductPackLabel label = productPackLabelService.getOne(queryWrapper);
        if (label==null){
          return JsonResult.success();
        }else {
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "打印条形码")
    @GetMapping("/getBarCode")
    public JsonResult<String> getBarCode(@RequestParam(value = "code") String code){

        String data = productPackLabelService.getBarCode(code);
        return JsonResult.success("true",data);
    }

}
